package com.dzf.controller;

import com.dzf.entity.*;
import com.dzf.service.LargeFileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



/**
 * @author DZF
 *
 */
@RestController
@RequestMapping("/api/public")
public class PublicController {

    @Autowired
    LargeFileService largeFileService;


    /**
     * 图片显示的方法
     * @param id        图片ID
     * @param response  响应流
     */
    @RequestMapping("/showImg/{id}")
    public void showImg(@PathVariable("id")String id,
                            HttpServletResponse response)
    {
        System.out.println(id);
        LargeFile largeFile = largeFileService.findByPic(id);
        if (largeFile==null)return;//对象为空则结束方法
        try {
            response.setContentType("image/jpeg");//将图片以jpg形式返回
            response.getOutputStream().write((byte[]) largeFile.getContent());//获取response的输出流，写出图片的内容
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }
}
